<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="25%" :title="`${drawerProps.title}-延迟队列`">
    <el-form ref="ruleFormRef" label-width="100px" label-suffix=":" :rules="rules" :model="operateDataInfo">
      <!-- 表单内容-->
      <el-form-item label="队列名称" prop="name">
        <el-input
          v-model="operateDataInfo.name"
          :disabled="drawerProps.isUpdate"
          placeholder="请输入队列名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="预加载间隔" prop="preloadDistance">
        <el-input-number v-model="operateDataInfo.preloadDistance" :min="1" :max="operateDataInfo.preloadDuration" clearable />
      </el-form-item>
      <el-form-item label="预加载时长" prop="preloadDuration">
        <el-input-number v-model="operateDataInfo.preloadDuration" :min="1" clearable />
      </el-form-item>
      <el-form-item label="时间单位" prop="timeUnit">
        <el-select v-model="operateDataInfo.timeUnit" filterable placeholder="请选择时间单位">
          <el-option
            v-for="item in drawerProps.enumMap?.get('timeUnit')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import {
  DelayQueueMetaPageResult,
  DelayQueueMetaSaveParam,
  DelayQueueMetaUpdateParam,
  getDelayQueueMetaInfo
} from "@/api/modules/queue/DelayQueueMeta";

interface DrawerProps {
  title: string;
  isSave: boolean;
  isUpdate: boolean;
  row: Partial<DelayQueueMetaPageResult>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  enumMap?: Map<string, { [key: string]: any }[]>;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  title: "",
  isSave: false,
  isUpdate: false,
  row: {}
});

const rules = reactive({
  // 表单规则, 例如: userName: [{ required: true, message: "请输入用户名" }]
  name: [{ required: true, message: "请输入队列名称" }]
});

const operateDataInfo = ref<DelayQueueMetaSaveParam | DelayQueueMetaUpdateParam>({});
const openDrawer = async (params: DrawerProps) => {
  if (params.isUpdate) {
    // 更新信息，先查询
    let { data } = await getDelayQueueMetaInfo(params.row.id ? params.row.id : "");
    operateDataInfo.value = { ...data };
  }

  if (params.isSave) {
    // 新增前的默认值
    operateDataInfo.value = { preloadDistance: 5, preloadDuration: 10, timeUnit: 5 };
  }
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(operateDataInfo.value);
      ElMessage.success({ message: `${drawerProps.value.title} 成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  openDrawer
});
</script>

<style scoped lang="scss"></style>
